﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows;

namespace Cheems.Control.Extensions
{
    public class UIElementExtensions
    {
        #region 鼠标进入放大动画
        /// <summary>
        /// 定义附加属性：是否放大
        /// </summary>
        public static readonly DependencyProperty IsMouseOverEnlargeProperty =
            DependencyProperty.RegisterAttached(
                "IsMouseOverEnlarge",
                typeof(bool),
                typeof(UIElementExtensions),
                new PropertyMetadata(false, OnIsMouseOverEnlargeChanged));

        /// <summary>
        /// 定义附加属性：放大倍率
        /// </summary>
        public static readonly DependencyProperty ScaleFactorProperty =
            DependencyProperty.RegisterAttached(
                "ScaleFactor",
                typeof(double),
                typeof(UIElementExtensions),
                new PropertyMetadata(1.1));  // 默认放大倍率 1.1

        /// <summary>
        /// 定义附加属性：动画持续时间
        /// </summary>
        public static readonly DependencyProperty EnlargeDurationProperty =
            DependencyProperty.RegisterAttached(
                "EnlargeDuration",
                typeof(double),
                typeof(UIElementExtensions),
                new PropertyMetadata(0.2));  // 默认动画持续时间 0.2 秒

        /// <summary>
        /// 附加属性的setter
        /// </summary>
        /// <param name="element"></param>
        /// <param name="value"></param>
        public static void SetIsMouseOverEnlarge(UIElement element, bool value)
        {
            element.SetValue(IsMouseOverEnlargeProperty, value);
        }
        /// <summary>
        /// 附加属性的 getter
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static bool GetIsMouseOverEnlarge(UIElement element)
        {
            return (bool)element.GetValue(IsMouseOverEnlargeProperty);
        }

        // 获取和设置 ScaleFactor
        public static void SetScaleFactor(UIElement element, double value)
        {
            element.SetValue(ScaleFactorProperty, value);
        }

        public static double GetScaleFactor(UIElement element)
        {
            return (double)element.GetValue(ScaleFactorProperty);
        }

        // 获取和设置 Duration
        public static void SetDuration(UIElement element, double value)
        {
            element.SetValue(EnlargeDurationProperty, value);
        }

        public static double GetDuration(UIElement element)
        {
            return (double)element.GetValue(EnlargeDurationProperty);
        }

        // 属性改变时的回调
        private static void OnIsMouseOverEnlargeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is UIElement element)
            {
                // 设置变化中心
                element.RenderTransformOrigin = new Point(0.5, 0.5);
                // 为控件添加鼠标事件处理
                if ((bool)e.NewValue)
                {
                    element.MouseEnter += Element_MouseEnter;
                    element.MouseLeave += Element_MouseLeave;
                }
                else
                {
                    element.MouseEnter -= Element_MouseEnter;
                    element.MouseLeave -= Element_MouseLeave;
                }
            }
        }

        // 鼠标进入时放大控件
        private static void Element_MouseEnter(object sender, MouseEventArgs e)
        {
            if (sender is UIElement element)
            {
                // 获取附加属性
                double scaleFactor = GetScaleFactor(element);  // 获取放大倍率
                double duration = GetDuration(element);  // 获取动画持续时间

                // 如果没有添加 ScaleTransform，则创建一个
                if (element.RenderTransform is not ScaleTransform scaleTransform)
                {
                    scaleTransform = new ScaleTransform(1, 1); // 初始大小
                    element.RenderTransform = scaleTransform;
                }

                // 创建放大的动画
                var scaleXAnimation = new DoubleAnimation(1, scaleFactor, TimeSpan.FromSeconds(duration));
                var scaleYAnimation = new DoubleAnimation(1, scaleFactor, TimeSpan.FromSeconds(duration));

                scaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty, scaleXAnimation);
                scaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty, scaleYAnimation);
            }
        }

        /// <summary>
        /// 鼠标离开时平滑缩小控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Element_MouseLeave(object sender, MouseEventArgs e)
        {
            if (sender is UIElement element)
            {
                // 获取附加属性
                double duration = GetDuration(element);  // 获取动画持续时间

                // 获取 ScaleTransform
                if (element.RenderTransform is ScaleTransform scaleTransform)
                {
                    // 创建缩小的动画
                    var scaleXAnimation = new DoubleAnimation(scaleTransform.ScaleX, 1, TimeSpan.FromSeconds(duration));
                    var scaleYAnimation = new DoubleAnimation(scaleTransform.ScaleY, 1, TimeSpan.FromSeconds(duration));

                    scaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty, scaleXAnimation);
                    scaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty, scaleYAnimation);
                }
            }
        }

        #endregion

        #region 鼠标进入透明度变化

        /// <summary>
        /// 定义附加属性：是否改变透明度
        /// </summary>
        public static readonly DependencyProperty IsMouseOverOpacityProperty =
            DependencyProperty.RegisterAttached(
                "IsMouseOverOpacity",
                typeof(bool),
                typeof(UIElementExtensions),
                new PropertyMetadata(false, OnIsMouseOverOpacityChanged));

        /// <summary>
        /// 定义附加属性：透明度
        /// </summary>
        public static readonly DependencyProperty OpacityFactorProperty =
            DependencyProperty.RegisterAttached(
                "OpacityFactor",
                typeof(double),
                typeof(UIElementExtensions),
                new PropertyMetadata(0.5));  // 默认透明度 0.5

        /// <summary>
        /// 定义附加属性：动画持续时间
        /// </summary>
        public static readonly DependencyProperty OpacityDurationProperty =
            DependencyProperty.RegisterAttached(
                "OpacityDuration",
                typeof(double),
                typeof(UIElementExtensions),
                new PropertyMetadata(0.05));  // 默认动画持续时间 0.05 秒

        /// <summary>
        /// 附加属性的setter
        /// </summary>
        /// <param name="element"></param>
        /// <param name="value"></param>
        public static void SetIsMouseOverOpacity(UIElement element, bool value)
        {
            element.SetValue(IsMouseOverOpacityProperty, value);
        }

        /// <summary>
        /// 附加属性的getter
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static bool GetIsMouseOverOpacity(UIElement element)
        {
            return (bool)element.GetValue(IsMouseOverOpacityProperty);
        }

        /// <summary>
        /// 获取和设置透明度
        /// </summary>
        public static void SetOpacityFactor(UIElement element, double value)
        {
            element.SetValue(OpacityFactorProperty, value);
        }

        public static double GetOpacityFactor(UIElement element)
        {
            return (double)element.GetValue(OpacityFactorProperty);
        }

        /// <summary>
        /// 获取和设置透明度动画持续时间
        /// </summary>
        public static void SetOpacityDuration(UIElement element, double value)
        {
            element.SetValue(OpacityDurationProperty, value);
        }

        public static double GetOpacityDuration(UIElement element)
        {
            return (double)element.GetValue(OpacityDurationProperty);
        }

        /// <summary>
        /// 属性改变时的回调
        /// </summary>
        private static void OnIsMouseOverOpacityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is UIElement element)
            {
                // 为控件添加鼠标事件处理
                if ((bool)e.NewValue)
                {
                    element.MouseEnter += Element_MouseEnterOpacity;
                    element.MouseLeave += Element_MouseLeaveOpacity;
                }
                else
                {
                    element.MouseEnter -= Element_MouseEnterOpacity;
                    element.MouseLeave -= Element_MouseLeaveOpacity;
                }
            }
        }

        /// <summary>
        /// 鼠标进入时改变透明度
        /// </summary>
        private static void Element_MouseEnterOpacity(object sender, MouseEventArgs e)
        {
            if (sender is UIElement element)
            {
                // 获取附加属性
                double opacityFactor = GetOpacityFactor(element);  // 获取透明度
                double duration = GetOpacityDuration(element);  // 获取动画持续时间

                // 创建透明度变化的动画
                var opacityAnimation = new DoubleAnimation(1, opacityFactor, TimeSpan.FromSeconds(duration));

                // 执行动画
                element.BeginAnimation(UIElement.OpacityProperty, opacityAnimation);
            }
        }

        /// <summary>
        /// 鼠标离开时恢复透明度
        /// </summary>
        private static void Element_MouseLeaveOpacity(object sender, MouseEventArgs e)
        {
            if (sender is UIElement element)
            {
                // 获取附加属性
                double duration = GetOpacityDuration(element);  // 获取动画持续时间

                // 创建恢复透明度的动画
                var opacityAnimation = new DoubleAnimation(element.Opacity, 1, TimeSpan.FromSeconds(duration));

                // 执行动画
                element.BeginAnimation(UIElement.OpacityProperty, opacityAnimation);
            }
        }

        #endregion

    }
}
